# Mission 5 Fence Patrol - final from botcore import * from time import sleep threshold = 2000 line_count = 0 def detect_line(n): is_detected = ls.read(n) > threshold leds.ls_num(n, is_detected) return is_detected def scan_lines(): got_line = False n = 0 while n < 5: if detect_line(n): got_line = True n = n + 1 return got_line def go_forward(): motors.run(LEFT, 45) motors.run(RIGHT, 45) def back_turn(): # Go backward motors.run(LEFT, -30) motors.run(RIGHT, -30) sleep(0.65) # Turn motors.run(LEFT, 30) motors.run(RIGHT, -30) sleep(0.65) # Safety feature while True: if buttons.was_pressed(0): break motors.enable(True) # == Main Program == while True: hit = scan_lines() if hit: back_turn() line_count = line_count + 1 if line_count == 256: line_count = 0 leds.user(line_count) else: go_forward()